<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      class="up_class"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="项目名称" prop="projectCode">
        <el-select
        @change="changeCode"
          style="width:100%"
          v-model="dataForm.projectCode"
          placeholder="请选择项目"
        >
          <el-option
            v-for="item in optionsprojectId"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备序列号" prop="deviceSn">
        <el-input
          v-model="dataForm.deviceSn"
          placeholder="设备序列号"
        ></el-input>
      </el-form-item>
      <el-form-item label="设备" prop="deviceId">
        <el-select
          style="width:100%"
          v-model="dataForm.deviceId"
          placeholder="请选择设备"
        >
          <el-option
            v-for="item in optionsdeviceId"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="违规类型" prop="violationType">
        <el-select
          style="width:100%"
          v-model="dataForm.violationType"
          placeholder="请选择违规类型"
        >
          <el-option
            v-for="item in optionsviolationType"
            :key="item.key"
            :label="item.value"
            :value="item.key"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="图片名称" prop="imageName">
        <el-input
          v-model="dataForm.imageName"
          placeholder="图片名称"
        ></el-input>
      </el-form-item> -->
      <el-form-item label="上传图片" prop="imageBase64">
        <!-- <el-input v-model="dataForm.imageBase64" placeholder="base64"></el-input> -->
        <el-upload
          class="avatar-uploader"
          :action="upUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :headers="headers"
        >
          <img v-if="imageBase64" :src="imageBase64" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="抓拍时间" prop="recordTime">
        <el-date-picker
          style="width:100%"
          value-format="yyyy-MM-dd HH:mm:ss"
          format="yyyy-MM-dd HH:mm:ss"
          v-model="dataForm.recordTime"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      headers:{token:this.$cookie.get('token')},
      upUrl: this.$http.adornUrl(`/sys/getParamenters/changeBase64`),
      optionsviolationType: [],
      optionsdeviceId: [],
      optionsprojectId: [],
      visible: false,
      imageBase64:"",
      dataForm: {
        id: 0,
        projectCode: "",
        deviceSn: "",
        deviceId: "",
        violationType: "",
        imageName: "",
        imageBase64: "",
        recordTime: ""
      },
      dataRule: {
        projectCode: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        deviceSn: [
          { required: true, message: "设备序列号不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备不能为空", trigger: "blur" }
        ],
        violationType: [
          { required: true, message: "违规类型不能为空", trigger: "blur" }
        ],
        imageName: [
          { required: true, message: "图片名称不能为空", trigger: "blur" }
        ],
        imageBase64: [
          { required: true, message: "base64不能为空", trigger: "blur" }
        ],
        recordTime: [
          { required: true, message: "抓拍时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 选择项目
    changeCode(e){
    //获取设备
    this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getVideoDevice?projectCode=${e}`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsdeviceId = data.list;
        }
      });
    },
    handleAvatarSuccess(res, file) {
      console.log(file)
      if(res.msg==='success'){
        this.dataForm.imageBase64 = res.code;
        this.imageBase64="data:image/png;base64,"+res.code
        this.dataForm.imageName=file.name
      }
    
    },
    beforeAvatarUpload(file) {},
    // 获取项目
    getCompanyTypes() {
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getProjectList`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsprojectId = data.list;
        }
      });
  
      //获取违规类型
      this.$http({
        url: this.$http.adornUrl(`/sys/getParamenters/getViolationType`),
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.optionsviolationType = data.list;
        }
      });
    },
    init(id) {
      this.getCompanyTypes();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.imageBase64=""
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/bviolationdata/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.projectCode = data.bViolationData.projectCode;
              this.changeCode(data.bViolationData.projectCode)
              this.dataForm.deviceSn = data.bViolationData.deviceSn;
              this.dataForm.deviceId =Number( data.bViolationData.deviceId);
              this.dataForm.violationType = data.bViolationData.violationType;
              this.dataForm.imageName = data.bViolationData.imageName;
              this.dataForm.imageBase64 = data.bViolationData.imageBase64;
              this.imageBase64 = data.bViolationData.imageBase64;
              this.dataForm.recordTime = data.bViolationData.recordTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/bviolationdata/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              projectCode: this.dataForm.projectCode,
              deviceSn: this.dataForm.deviceSn,
              deviceId: this.dataForm.deviceId,
              violationType: this.dataForm.violationType,
              imageName: this.dataForm.imageName,
              imageBase64: this.dataForm.imageBase64,
              recordTime: this.dataForm.recordTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
<style>
.up_class .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.up_class .avatar-uploader .el-upload:hover {
  border-color: #17B3A3;
}
.up_class .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.up_class .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
